package com.inossem.allotin.utils.crashException;

import android.content.Context;
import android.os.Looper;

import com.inossem.allotin.utils.logger.LogFile;
import com.inossem.allotin.utils.logger.LogUtils;
import com.orhanobut.logger.Logger;

import java.lang.Thread.UncaughtExceptionHandler;

public class CrashExceptionHandler implements UncaughtExceptionHandler {

    public static final String LOG_NAME_CRASHEXCEPTION = "CrashException";// 崩溃日志名称
    public static final String NEW_LINE = "\n\n";
    public static final String TAG_INOSSEM = "INOSSEM";

    private StringBuffer sb = new StringBuffer();// 保存异常信息

    private UncaughtExceptionHandler uncaughtExceptionHandler;

    private static CrashExceptionHandler carshExceptionHandler;

    private CrashExceptionUtils.CrashExceptionResponse mCrashExceptionResponse;

    private CrashExceptionHandler() {
    }

    private Context mContext;

    /**
     * 获取CrashExceptionHandler对象
     *
     * @return CrashExceptionHandler对象
     * @author leij
     */
    public static synchronized CrashExceptionHandler getInstance() {
        if (carshExceptionHandler == null) {
            carshExceptionHandler = new CrashExceptionHandler();
        }
        return carshExceptionHandler;
    }

    /**
     * 初始化
     *
     * @param context                上下文
     * @param crashExceptionResponse 捕获异常响应监听
     * @author leij
     */
    public void init(Context context, CrashExceptionUtils.CrashExceptionResponse crashExceptionResponse) {
        // 获取默认异常捕获
        uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
        this.mContext = context;
        this.mCrashExceptionResponse = crashExceptionResponse;
    }

    /**
     * 未捕获的异常
     *
     * @param thread 线程对象
     * @param ex     异常对象
     * @author leij
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            // 处理捕获的异常
            crashException(mContext, ex);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            uncaughtExceptionHandler.uncaughtException(thread, ex);
        }
        // 回调函数
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                mCrashExceptionResponse.onResponse(mContext);
                Looper.loop();
            }
        }.start();
    }

    /**
     * 捕获异常
     *
     * @param context 上下文
     * @param ex      异常对象
     * @author leij
     */
    private void crashException(Context context, Throwable ex) {
        // 获取APP版本，查询设备基本信息，获取崩溃信息，保存文件
        collectCrashInfo(context, ex);
        Logger.e(sb.toString()); // 打印日志
        LogFile.saveLog(context, LOG_NAME_CRASHEXCEPTION, sb.toString() + NEW_LINE
                + LogUtils.exceptionToString(null));
    }

    /**
     * 收集异常信息
     *
     * @param context 上下文
     * @param ex      异常对象
     * @author leij
     */
    private void collectCrashInfo(Context context, Throwable ex) {
        // 正常打印异常
        ex.printStackTrace();
        sb.append(LogUtils.exceptionToString(ex));

    }

}
